package codepulse.blogmoon.cn.code.process.impl.gitea;

import codepulse.blogmoon.cn.code.constant.GiteaConstant;
import codepulse.blogmoon.cn.code.dto.CodeAccountDetailDTO;
import codepulse.blogmoon.cn.code.entity.CodeAcctDetail;
import codepulse.blogmoon.cn.code.enums.SourceEnum;
import codepulse.blogmoon.cn.code.process.AbstractCodeAccountProcess;
import codepulse.blogmoon.cn.code.process.CodeAccountProcess;
import codepulse.blogmoon.cn.code.service.CodeAcctDetailService;
import codepulse.blogmoon.cn.code.utils.GiteaRestClient;
import codepulse.blogmoon.cn.code.vo.gitea.CreateUserReq;
import codepulse.blogmoon.cn.code.vo.gitea.CreateUserResp;
import codepulse.blogmoon.cn.system.dto.SysAcctInfoDTO;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class GiteaCodeAccountProcessImpl extends AbstractCodeAccountProcess {

    @Resource
    private GiteaRestClient giteaRestClient;

    @Resource
    private CodeAcctDetailService codeAcctDetailService;

    @Override
    public SourceEnum getSource() {
        return SourceEnum.GITEA;
    }

    @Override
    public void createCodeAccount(CodeAccountDetailDTO dto) {
        CreateUserReq req = CreateUserReq.builder().loginName(dto.getMobile())
                .password(dto.getPassword())
                .email(dto.getEmail())
                .fullName(dto.getRealName())
                .username(dto.getMobile())
                .build();
        CreateUserResp resp = giteaRestClient.post(GiteaConstant.ADMIN_USER_URL, req, CreateUserResp.class);
        CodeAcctDetail codeAcctDetail = new CodeAcctDetail();
        codeAcctDetail.setBizAcctId(dto.getBizAcctId());
        codeAcctDetail.setLoginName(resp.getLoginName());
        codeAcctDetail.setPassword(dto.getPassword());
        codeAcctDetail.setExtra(JSON.toJSONString(resp));
        codeAcctDetailService.save(codeAcctDetail);
    }
}
